#include "StdAfx.h"
#include "FileWriter.h"

//////////////////////////////////////////////////////////////////////////
//FileWriter definition
//////////////////////////////////////////////////////////////////////////
FileWriter::FileWriter( std::string fileName )
{
	mFileName = fileName;
}


FileWriter::~FileWriter(void)
{
}

bool FileWriter::openFile()
{
	mFileOutStream = std::ofstream(mFileName.c_str());
	if (!mFileOutStream.good())
	{
		Helper::errInfoDialog(L"failed create file");
		return false;
	}
	return true;
}

void FileWriter::writeStringToFile( std::string dataStr )
{
	mFileOutStream << dataStr;
}

void FileWriter::writeStringToFile( std::string dataStr, int setWidth )
{
	mFileOutStream << std::right			  //right aligned
				   << std::setw(setWidth)	 //use manipulator
				   << dataStr				 //data
 				   << std::string("   \t");
	mFileOutStream.flush();
				   
}

void FileWriter::flushAndNewLine()
{
	mFileOutStream<<std::endl;
}

//////////////////////////////////////////////////////////////////////////
//DataFileWriter definition
//////////////////////////////////////////////////////////////////////////
DataFileWriter::DataFileWriter(std::string fileName) : FileWriter(fileName)
{
}

DataFileWriter::~DataFileWriter()
{
}

void DataFileWriter::writeFileHeader()
{
	writeStringToFile("record start at: ");
	writeStringToFile(Helper::getCurrentTime());
	flushAndNewLine();
	flushAndNewLine();
	writeStringToFile("Gyr_x", 10);
	writeStringToFile("Gyr_y", 10);
	writeStringToFile("Gyr_z", 10);
	writeStringToFile("Acc_x", 10);
	writeStringToFile("Acc_y", 10);
	writeStringToFile("Acc_z", 10);
	writeStringToFile("Mag_x", 10);
	writeStringToFile("Mag_y", 10);
	writeStringToFile("Mag_z", 10);
	writeStringToFile("Roll", 11);
	writeStringToFile("Pitch", 10);
	writeStringToFile("yaw", 10);
	flushAndNewLine();
}

//////////////////////////////////////////////////////////////////////////
//LogFileWriter definition
//////////////////////////////////////////////////////////////////////////
LogFileWriter::LogFileWriter(std::string fileName) : FileWriter(fileName)
{
}

LogFileWriter::~LogFileWriter()
{
}

void LogFileWriter::writeFileHeader()
{
	writeStringToFile("User try connect.");
	flushAndNewLine();
}

void LogFileWriter::writeStringToFile( std::string dataStr )
{
	mFileOutStream << Helper::getCurrentTime() 
				   << std::string(":  ")
				   << dataStr;
}